'use strict';
/**
 * Created by FreezeSoul on 2015/10/11.
 */
define([
    'angular',
    'app/modules/folder/define',
    'app/modules/folder/services/folder-tree'
],function(angular) {
    angular.module('ad.folder')
        .controller('adFolderEditController',['$scope','$uibModalInstance','adFolderEditService','item','type','action'
            ,function($scope,$uibModalInstance,service,item,type,action) {
                if(action == "folderAdd"){
                    $scope.item = {
                        folder_name :'',
                        folder_type : type,
                        folder_parent: item.data.folder_id
                    };
                }else{
                    $scope.item = item.data;
                }

                $scope.ok = function () {
                    if(action == "folderAdd"){
                        service.addFolder($scope.item.folder_name,$scope.item.folder_parent,$scope.item.folder_type).then(function(data){
                            $uibModalInstance.close(data);
                        });
                    }else {
                        service.updateFolder($scope.item.folder_id,$scope.item.folder_name,$scope.item.folder_type).then(function (data) {
                            $uibModalInstance.close(data);
                        });
                    }
                };

                $scope.cancel = function () {
                    $uibModalInstance.dismiss('cancel');
                };
            }])
        .controller('adFolderDeleteController',['$scope','$uibModalInstance','adFolderEditService','item','type'
            ,function($scope,$uibModalInstance,service,item) {
            $scope.item = item;

            $scope.ok = function () {
                if(item.children.length == 0){
                    service.removeFolder($scope.item.data.folder_id,$scope.item.data.folder_type).then(function(data){
                        $uibModalInstance.close(data);
                    });
                }else{
                    $uibModalInstance.dismiss("cancel");
                }
            };

            $scope.cancel = function () {
                $uibModalInstance.dismiss('cancel');
            };
        }]);
});